<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="名称" prop="thirdColumnName">
                <el-input
                        v-model="queryParams.thirdColumnName"
                        placeholder="请输入名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="权益类型" prop="thirdColumnType">
                <el-select v-model="queryParams.thirdColumnType" placeholder="请选择权益类型" clearable size="small">
                    <el-option
                            v-for="item in thirdColumnTypeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="表单方式" prop="formType">
                <el-select v-model="queryParams.formType" placeholder="请选择充值表单方式" clearable size="small">
                    <el-option
                            v-for="item in formTypeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="同步状态" prop="thirdColumnType">
                <el-select v-model="queryParams.thirdColumnSyscstate" placeholder="请选择同步状态" clearable size="small">
                    <el-option
                            v-for="item in thirdColumnSyscstateOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['system:column:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['system:column:edit']"
                >修改
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="columnList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" type="index" width="50" align="center">
                <template slot-scope="scope">
                    <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="名称" align="center" prop="thirdColumnName"/>
            <el-table-column label="权益类型" align="center" prop="thirdColumnType">
                <template scope="scope">
                    <span v-if="scope.row.thirdColumnType=='11'">影视会员</span>
                    <span v-if="scope.row.thirdColumnType=='12'">音乐会员</span>
                    <span v-if="scope.row.thirdColumnType=='13'">在线教育</span>
                    <!--          <span v-if="scope.row.thirdColumnType=='14'">肯德基</span>
                              <span v-if="scope.row.thirdColumnType=='15'">麦当劳</span>-->
                    <span v-if="scope.row.thirdColumnType=='16'">交通出行</span>
                    <span v-if="scope.row.thirdColumnType=='17'">美食饮品</span>
                    <span v-if="scope.row.thirdColumnType=='18'">酒店旅游</span>
                </template>
            </el-table-column>
            <el-table-column label="权益状态" align="center" prop="thirdColumnState">
                <template scope="scope">
                    <span v-if="scope.row.thirdColumnState=='0'">下架</span>
                    <span v-if="scope.row.thirdColumnState=='1'">上架</span>
                </template>
            </el-table-column>
            <el-table-column label="同步状态" align="center" prop="thirdColumnSyscstate">
                <template scope="scope">
                    <span v-if="scope.row.thirdColumnSyscstate==0">未同步</span>
                    <span v-if="scope.row.thirdColumnSyscstate==1">已同步</span>
                </template>
            </el-table-column>
            <el-table-column label="图标" align="center" prop="thirdColumnIcon">
                <template scope="scope">
                    <el-popover placement="right" title="" trigger="hover">
                        <img :src="scope.row.thirdColumnIcon | imgPathFilter" style="width: auto;">
                        <img slot="reference" :src="scope.row.thirdColumnIcon | imgPathFilter"
                             :alt="scope.row.thirdColumnIcon | imgPathFilter"
                             style="max-height: 50px;max-width: 130px"
                        >
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="描述" align="center" prop="thirdColumnIntroduce"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="320px">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:column:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleManageCategory(scope.row)"
                    >管理分类
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleManageProduct(scope.row)"
                    >管理商品
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            @click="handleSyscProduct(scope.row)"
                            v-if="scope.row.thirdColumnSyscstate==0"
                    >同步数据
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            @click="handleUpdateWiKeYunProduct(scope.row)"
                    >同步价格
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改权益配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="名称" prop="thirdColumnName">
                    <el-input v-model="form.thirdColumnName" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="权益类型" prop="thirdColumnType">
                    <el-select v-model="form.thirdColumnType" placeholder="请选择权益类型" clearable size="small">
                        <el-option
                                v-for="item in thirdColumnTypeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="thirdColumnType">
                    <el-select v-model="form.thirdColumnState" placeholder="请选择状态" clearable size="small">
                        <el-option
                                v-for="item in thirdColumnStateOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="图标" prop="thirdColumnIcon">
                    <el-upload
                            class="avatar-uploader"
                            :action="upload.url"
                            :show-file-list="false"
                            :headers="upload.headers"
                            name="file"
                            accept=".jpg, .png"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="form.thirdColumnIcon" :src="form.thirdColumnIcon| imgPathFilter" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="表单方式" prop="formType">
                    <el-select v-model="form.formType" placeholder="请选择充值表单方式" clearable size="small">
                        <el-option
                                v-for="item in formTypeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="微客云权益Id" prop="wikeyunServerid" label-width="150px">
                    <el-input v-model="form.wikeyunServerid" placeholder="请输入权益Id"/>
                </el-form-item>
                <el-form-item label="分类简介" prop="thirdColumnDesc">
                    <el-input v-model="form.thirdColumnIntroduce" placeholder="请输入分类简介"/>
                </el-form-item>
                <el-form-item label="充值类型" prop="thirdColumnDesc">
                    <el-input v-model="form.thirdColumnUse" placeholder="请输入充值类型"/>
                </el-form-item>
                <el-form-item label="充值说明" prop="thirdColumnDesc">
                    <editor v-model="form.thirdColumnDesc" :min-height="192"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog title="权益分类管理" :visible.sync="openCategory" v-if="openCategory">
            <category-index :thirdColumnId="thirdColumnId"></category-index>
        </el-dialog>
        <el-dialog title="产品管理" :visible.sync="openProduct" v-if="openProduct">
            <product-index :thirdColumnId="thirdColumnId" :productType="productType"></product-index>
        </el-dialog>
    </div>
</template>

<script>
import Editor from '@/components/Editor'
import { getToken } from '@/utils/auth'
import productIndex from './product/index'
import categoryIndex from './thirdColumnCategory/index'
import { listColumn, getColumn, delColumn, addColumn, updateColumn, exportColumn } from '@/api/app/thirdColumn'
import { syscProduct, updateWiKeYunProduct } from '@/api/app/product'

export default {
    name: 'Column',
    components: { productIndex, categoryIndex, Editor },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 权益配置表格数据
            columnList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 是否显示管理产品列表
            openProduct: false,
            thirdColumnId: '',
            productType: '',
            // 是否显示权益管理列表
            openCategory: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                thirdColumnName: null,
                thirdColumnType: null,
                formType: 1,
                thirdColumnState: null
            },
            // 上传参数
            upload: {
                // 是否禁用上传
                isUploading: false,
                // 设置上传的请求头部
                headers: { Authorization: 'Bearer ' + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + '/common/upload',
                // 上传的文件列表
                fileList: []
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                thirdColumnName: [
                    { required: true, message: '名称不能为空', trigger: 'blur' }
                ],
                thirdColumnType: [
                    { required: true, message: '类型不能为空', trigger: 'blur' }
                ],
                wikeyunServerid: [
                    { required: true, message: '微客运权益不能为空', trigger: 'blur' }
                ],
                formType: [
                    { required: true, message: '表单类型不能为空', trigger: 'blur' }
                ],
                thirdColumnIntroduce: [
                    { required: true, message: '简介不能为空', trigger: 'blur' }
                ],
                thirdColumnDesc: [
                    { required: true, message: '描述不能为空', trigger: 'blur' }
                ]
            },
            thirdColumnTypeOptions: [
                { value: 11, label: '影视会员' },
                { value: 12, label: '音乐会员' },
                { value: 13, label: '在线教育' },
                /* { value: '14', label: '肯德基' },
                 { value: '15', label: '麦当劳' },*/
                { value: 16, label: '交通出行' },
                { value: 17, label: '美食饮品' },
                { value: 18, label: '酒店旅游' }
            ],
            thirdColumnStateOptions: [
                { value: '0', label: '下架' },
                { value: '1', label: '上架' }
            ],
            formTypeOptions: [
                { value: 1, label: '文字' },
                { value: 2, label: '图片' }
            ],
            thirdColumnSyscstateOptions: [
                { value: 0, label: '未同步' },
                { value: 1, label: '已同步' }
            ]
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询权益配置列表 */
        getList() {
            this.loading = true
            listColumn(this.queryParams).then(response => {
                this.columnList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                thirdColumnId: null,
                thirdColumnName: null,
                thirdColumnType: null,
                thirdColumnState: null,
                thirdColumnSyscstate: null,
                thirdColumnIcon: null,
                wikeyunServerid: null,
                thirdColumnIntroduce: null,
                thirdColumnUse: null,
                thirdColumnDesc: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            // 查询参数
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                thirdColumnName: null,
                thirdColumnType: null,
                thirdColumnState: null
            },
                    this.handleQuery()

        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.thirdColumnId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加权益配置'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const thirdColumnId = row.thirdColumnId || this.ids
            getColumn(thirdColumnId).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改权益配置'
            })
        },
        /*弹窗打开分类列表*/
        handleManageCategory(row) {
            this.openCategory = true
            this.thirdColumnId = row.thirdColumnId
        },
        /*弹窗打开商品列表*/
        handleManageProduct(row) {
            this.openProduct = true
            this.thirdColumnId = row.thirdColumnId
            this.productType = row.thirdColumnType
        },
        /*同步分类及商品*/
        handleSyscProduct(row) {
            let thirdColumnId = row.thirdColumnId
            this.$confirm('是否确认从微客云集同步"' + row.thirdColumnName + '"的分类及产品数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return syscProduct({ thirdColumnId: thirdColumnId })
            }).then(() => {
                this.getList()
                this.msgSuccess('同步成功')
            })
        },
        /*同步分类及商品*/
        handleUpdateWiKeYunProduct(row) {
            let thirdColumnId = row.thirdColumnId
            this.$confirm('是否确认从微客云集同步"' + row.thirdColumnName + '"的产品价格数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return updateWiKeYunProduct({ thirdColumnId: thirdColumnId })
            }).then(() => {
                this.getList()
                this.msgSuccess('同步产品价格成功')
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.thirdColumnId != null) {
                        updateColumn(this.form).then(response => {
                            this.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addColumn(this.form).then(response => {
                            this.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const thirdColumnIds = row.thirdColumnId || this.ids
            this.$confirm('是否确认删除权益配置编号为"' + thirdColumnIds + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return delColumn(thirdColumnIds)
            }).then(() => {
                this.getList()
                this.msgSuccess('删除成功')
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams
            this.$confirm('是否确认导出所有权益配置数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return exportColumn(queryParams)
            }).then(response => {
                this.download(response.msg)
            })
        },
        handleAvatarSuccess(res, file) {
            this.form.thirdColumnIcon = res.data.objectID
        },
        beforeAvatarUpload(file) {
            const isLt5M = file.size / 1024 / 1024 < 5
            if (!isLt5M) {
                this.$message.error('上传导航图标大小不能超过 5MB!')
            }
            return isLt5M
        }
    }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
